package linestrategy

import (
	"codejy/analysis/lineanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"strings"
)

// 类型行为内使用this.方法调用策略
type ThisMethodStrategy struct {
}

// 注册策略
func init() {
	RegisterStrategy(&ThisMethodStrategy{})
}

// 判断
func (f *ThisMethodStrategy) getId() int {
	return lineanalysis.ThisMethod
}

// 调用此方法必然在实例内部方法中,所以CurrentInstanceAddress不会为空
func (f *ThisMethodStrategy) Execute(threadStore *structdef.ThreadStore) {
	content := threadStore.Code.LineContent
	index1 := strings.Index(content, ".")
	methodName := content[index1+1:] //方法名称
	//获取类型方法定义
	currentInstanceAddress := threadStore.CurrentInstanceAddress
	prop, _ := store.GetInstanceProp(threadStore, currentInstanceAddress)
	typeName, _ := prop["@TYPENAME@"]  //获取实例的类型名称
	scope, _ := prop["@TYPEFILEPATH@"] //获取实例的类型名称
	typeDefinition := store.GetTypeDefinition(scope.Value, typeName.Value)
	funName, funParams := SubFunctionCallNameAndParam(methodName)
	definition, p := typeDefinition.Methods[funName]
	if p {
		globalfunc.ExecuteFunc("ExecuteTypeMethod", threadStore, funName, definition, funParams)
	} else {
		panic("错误::::::" + typeName.Value + "类型中没有" + content + "行为方法的定义")
	}
}
